/*
 * Copyright (C) 2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include <stdlib.h>
#include <check.h>
#include "PlanetWars2dRT-core/utils/arguments_management.h"


START_TEST(test_option_short)
{
  ck_assert(is_a_short_cstring_option_unsafe("-h"));
  ck_assert(is_a_short_cstring_option_unsafe("-v"));
  ck_assert(is_a_short_cstring_option_unsafe("-?"));
  
  ck_assert(!is_a_short_cstring_option_unsafe("--help"));
  ck_assert(!is_a_short_cstring_option_unsafe("help"));
  ck_assert(!is_a_short_cstring_option_unsafe("h"));
  ck_assert(!is_a_short_cstring_option_unsafe("+h"));
  ck_assert(!is_a_short_cstring_option_unsafe("=h"));
}
END_TEST

START_TEST(test_option_long)
{
  ck_assert(is_a_long_cstring_option_unsafe("--help"));
  ck_assert(is_a_long_cstring_option_unsafe("--version"));
  ck_assert(is_a_long_cstring_option_unsafe("--need-help"));
  
  ck_assert(!is_a_long_cstring_option_unsafe("==help"));
  ck_assert(!is_a_long_cstring_option_unsafe("++help"));
  ck_assert(!is_a_long_cstring_option_unsafe("help--"));
  ck_assert(!is_a_long_cstring_option_unsafe("-help"));
  ck_assert(!is_a_long_cstring_option_unsafe("help"));
  ck_assert(!is_a_long_cstring_option_unsafe("-h"));
  ck_assert(!is_a_long_cstring_option_unsafe("h"));
  ck_assert(!is_a_long_cstring_option_unsafe("-need-help"));
}
END_TEST

void
test_option_help_generic(bool (*is_help_option)(const char* arg))
{
  ck_assert(is_help_option("-?"));
  ck_assert(is_help_option("-h"));
  ck_assert(is_help_option("--help"));
  ck_assert(is_help_option("--info"));
  
  ck_assert(!is_help_option("h"));
  ck_assert(!is_help_option("help"));
  ck_assert(!is_help_option("-o"));
  ck_assert(!is_help_option("--option"));
}

START_TEST(test_option_help_unsafe)
{
  test_option_help_generic(is_a_help_cstring_option_unsafe);
}
END_TEST

START_TEST(test_option_help)
{
  test_option_help_generic(is_a_help_cstring_option);
  ck_assert(!is_a_help_cstring_option(NULL));
}
END_TEST


Suite*
arguments_management_suite(void)
{
  Suite* s = suite_create("Arguments management");
  
  TCase* tc_option = tcase_create("Option");
  tcase_add_test(tc_option, test_option_short);
  tcase_add_test(tc_option, test_option_long);
  
  TCase* tc_help = tcase_create("Help");
  tcase_add_test(tc_help, test_option_help_unsafe);
  tcase_add_test(tc_help, test_option_help);
  suite_add_tcase(s, tc_help);
  
  return s;
}

int
main(void)
{
  size_t number_failed;
  Suite* s;
  SRunner* sr;

  s = arguments_management_suite();
  sr = srunner_create(s);
  
  srunner_run_all(sr, CK_NORMAL);
  number_failed = srunner_ntests_failed(sr);
  srunner_free(sr);
  
  return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
